package com.ruoyi.basis.utils.mp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mashuai.utils.toolkit.core.msCollection.MsCollectUtils;
import org.apache.commons.collections4.MapUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: PageUtils
 * @Description: 分页结果处理
 */
public class MpPageUtils {

    /**
     * 简化不走平台分页时候，避免重复创建page与size。
     *
     * @return
     * @author mashuai
     */

    public static <M extends BaseMapper, T> IPage<T> getPageBeanData(M m, Map<String, Object> paramsMap, LambdaQueryWrapper<T> qw) {
        IPage<T> iPage = m.selectPage(getPage(paramsMap), qw);
        return iPage;
    }

    public static <M extends BaseMapper, T> IPage<Map<String, Object>> getPageMapData(M m, Map<String, Object> paramsMap, LambdaQueryWrapper<T> qw) {
        IPage<Map<String, Object>> iPage = m.selectMapsPage(getPage(paramsMap), qw);
        return iPage;
    }

    public static Page getPage(Map<String, Object> paramsMap) {
        Integer page = MapUtils.getInteger(paramsMap, "pageNum", 1);
        Integer size = MapUtils.getInteger(paramsMap, "pageSize", 10);
        return new Page<>(page == null ? 1 : page, size == null ? 10 : size);
    }

    /**
     * App接口返回的分页数据格式
     *
     * @param iPage
     * @return
     * @author mashuai
     */

    public static <T> Map<String, Object> handlePageData(IPage<T> iPage) {
        //处理分页的数据
        Map<String, Object> resultMap = new HashMap<>(4);
        resultMap.put("data", iPage.getRecords());
        resultMap.put("size", iPage.getSize());
        resultMap.put("page", iPage.getCurrent());
        resultMap.put("total", iPage.getTotal());
        return resultMap;
    }

    /***
     *把旧分页配置的属性，拷贝给新的分页属性
     * @param oldIPage
     *
     * @return:
     * @author:mashuai
     */
    public static <Old, New> IPage<New> copyPageAttr(IPage<Old> oldIPage, Class<New> newClass) {
        IPage<New> newIPage = new Page<>();
        newIPage.setPages(oldIPage.getPages());
        newIPage.setCurrent(oldIPage.getCurrent());
        newIPage.setSize(oldIPage.getSize());
        newIPage.setTotal(oldIPage.getTotal());
        return newIPage;
    }

    /**
     * 自动分页
     *
     * @param data
     * @param current
     * @param size
     * @return
     * @author:mashuai
     */
    public static <T> Map<String, Object> autoPage(List<T> data, Integer current, Integer size) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("size", size);
        resultMap.put("current", current);
        Integer total = data.size();
        resultMap.put("total", total);
        if (data.size() == 0) return resultMap;
        List<List<T>> lists = MsCollectUtils.splitList(data, size > total ? total : size);
        resultMap.put("records", lists.get(current - 1));
        resultMap.put("pages", lists.size());
        return resultMap;
    }
}
